/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package compute;

import java.io.Serializable;
import java.util.Vector;

/**
 *
 * @author spider391Tang
 */
public class GridifyPrime implements Task<Long>, Serializable {

    public long prime; // 379
    public long min, max; // 2, 378

    long check_prime(long prime, long min, long max) {
        long count = 0;
        System.out.println("("+prime+","+min+","+max+")");
        for (long i = min; i <= max; i++) {
            if (prime % i == 0 && i!=1 && i!=prime) {
                System.out.println("The factor is " + i + ".!!");
                count++;
            }
        }
        return count;
     }

    public GridifyPrime() {
    }
    // Return a vector of tasks.

    public Vector<GridifyPrime> primeMapper(int num) {

        System.out.println("[primeMapper] : " + num);
        Vector<GridifyPrime> v = new Vector<GridifyPrime>();

        long interval = (max - min) / num;
        for (int i = 0; i < num - 1; i++) {
            GridifyPrime p = new GridifyPrime();
            String s = String.valueOf(prime) + " " + String.valueOf(min + (i * interval)) + " " + String.valueOf(min + ((i + 1) * interval) - 1);
            p.init(s);
            //System.out.println(s);
            v.add(p);
        }
        /// 公子乖 -　last one
        GridifyPrime p = new GridifyPrime();
        String s = String.valueOf(prime) + " " + String.valueOf(min + ((num-1) * interval)) + " " + String.valueOf(max);
        //System.out.println(s);
        p.init(s);
        v.add(p);

        for(int i=0;i< num;i++)
        {
             GridifyPrime pp = v.elementAt(i);
             System.out.println("("+pp.prime+","+pp.min+","+pp.max+")");
        }

        return v;
    }

    public Long primeReducer(Vector<Long> mapped_results) {

        long count = 0;
        //System.out.println("result size = " + mapped_results.size());
        for(int i=0;i<mapped_results.size();i++){
            System.out.println("[primeReducer] " + i + ": " + mapped_results.elementAt(i));
            count+=mapped_results.elementAt(i);
        }
        return count;
    }
//   @Debug(
//       value = "unit",
//       name = "debug1"
//    )

    public void init(String arg) {
        if(arg ==  null)
            return;
        //System.out.println("[init]==>" + arg );
        String names[] = arg.split(" ");
        if (names.length < 3) {
            System.out.println("init priGridifyPrime arg error");
            System.out.println("eq./task p1 GridifyPrime 127 1 127");
        }
        prime = Long.parseLong(names[0]);
        min = Long.parseLong(names[1]);
        max = Long.parseLong(names[2]);
    }

    @Gridify(mapper = "primeMapper", reducer = "primeReducer")
    public Long execute() {
        System.out.println("GridifyPrime.execute is called");
        return check_prime(prime, min, max);
    }
    //@Gridify(mapper = "primeMapper", reducer = "primeReducer")
}

